package com.yy.logic.impl;

import net.sf.json.JSONObject;














import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.yy.model.TbJob;
import com.yy.model.TbResume;
import com.yy.dao.IResumeDAO;
import com.yy.dto.JobDTO;
import com.yy.dto.ResumeDTO;
import com.yy.logic.IResumeLogic;
import com.yy.dao.ICompanyDAO;
import com.yy.dao.IJobDAO;
@Component
public class ResumeLogic implements IResumeLogic{
	
	@Resource
	private IResumeDAO resumeDAO;
	@Resource
	private ICompanyDAO companyDAO;
	@Resource
	private IJobDAO jobDAO;
	@Transactional
	@Override
	public JSONObject getAllResume(int jobid) throws Exception {
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		int count=0;
		try{
			 System.out.println("this is Resume logic******************111111111111111111111111");
			List<TbResume> listjobBean=this.resumeDAO.getAllResume(jobid);
			count=this.resumeDAO.getResumeCount(jobid);
			 System.out.println("this is Resume logic******************"+count);
			List<ResumeDTO> listjobDTO = new ArrayList<ResumeDTO>();
			Iterator<TbResume> itrjobBean = listjobBean.iterator();
			while(itrjobBean.hasNext()){
				TbResume tbjobBean=(TbResume)itrjobBean.next();
				listjobDTO.add(tbjobBean.toDTO());
			}
			jsonMap.put("data",listjobDTO);
			jsonMap.put("num", count);
		}catch(Exception e){
			e.printStackTrace();
		}
		return JSONObject.fromObject(jsonMap);
		
	}
	@Transactional
	@Override
	public int getCompanyId(String companyLoginName) throws Exception {
		// TODO Auto-generated method stub
		int result=0;
		try{
			System.out.print("CompanyId logic **************");
			result=this.resumeDAO.getCompanyId(companyLoginName);
		}catch(Exception e){
			e.printStackTrace();
			result=0;
		}
		return result;
	}
	
	@Transactional
	@Override
	public Integer addResume(ResumeDTO resumeDTO)throws Exception{
		Integer result = 0;
		try{
			result = this.resumeDAO.addResume(resumeDTO.toDAO());
		}catch(Exception e){
			e.printStackTrace();
			result = 0;
		}
		return result;
	}
	@Transactional
	@Override
	public Integer addCommand(int resumeId,String resumeState) throws Exception {
		// TODO Auto-generated method stub
		Integer result=0;
		try{
			result=this.resumeDAO.addCommand(resumeId,resumeState);
		}
		catch(Exception e){
			e.printStackTrace();
			result=0;
		}
		return result;
	}
	
	@Transactional
	public JSONObject getResumuByName(String loginName) throws Exception{
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		int count=0;
		try{
			 System.out.println("this is Resume logic******************111111111111111111111111");
			List<TbResume> listjobBean=this.resumeDAO.getResumuByName(loginName);
			count=this.resumeDAO.getNumByName(loginName);
			 System.out.println("this is Resume logic******************"+count);
			List<ResumeDTO> listjobDTO = new ArrayList<ResumeDTO>();
			Iterator<TbResume> itrjobBean = listjobBean.iterator();
			while(itrjobBean.hasNext()){
				TbResume tbjobBean=(TbResume)itrjobBean.next();
				System.out.println("+++++++"+tbjobBean.getJobId());
				//String companyName = this.companyDAO.getCompanyName(tbjobBean.getJobId());
				String jobName = this.jobDAO.getJobName(tbjobBean.getJobId());
				int companyId = this.jobDAO.getCompanyId(tbjobBean.getJobId()) ;
				String companyName = this.companyDAO.getCompanyName(companyId);
				System.out.println("--------+"+companyName);
				ResumeDTO resumeDTO2 = tbjobBean.toDTO();
				resumeDTO2.setJobName(jobName);
				resumeDTO2.setCompanyName(companyName);
				//listjobDTO.add(tbjobBean.toDTO());
				listjobDTO.add(resumeDTO2);
			}
			jsonMap.put("data",listjobDTO);
			jsonMap.put("num", count);
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println(JSONObject.fromObject(jsonMap).toString(1));
		return JSONObject.fromObject(jsonMap);
	}
	@Transactional
	public int delResumeById(int resumeId) throws Exception{
		int ret = 0;
		try{
			ret = this.resumeDAO.delResumeById(resumeId);
			//result=this.resumeDAO.addCommand(resumeId,resumeState);
		}
		catch(Exception e){
			e.printStackTrace();
			ret=0;
		}
		return ret;
	}

}
